package com.tdam2013.grupo07.data;

import java.util.Calendar;

import com.tdam2013.grupo07.util.Constantes;

import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Message;

public class ConectividadDBHandler extends Handler {
	
	private DatabaseHelper dbHelper; 
	public ConectividadDBHandler(DatabaseHelper dbHelper)
	{
		this.dbHelper= dbHelper;
	}
	@Override
	public void handleMessage(Message msg) {
		boolean noConnectivity=msg.getData().getBoolean(Constantes.EXTRA_NO_CONNECTIVITY, false);
		int networkInfo=msg.getData().getInt(Constantes.EXTRA_CONNECTIVITY_TYPE, -1);
		int conectividadEstado=noConnectivity? DatabaseHelper.CONECTIVITY_STATE_DISCONECTED:DatabaseHelper.CONECTIVITY_STATE_CONECTED;
		int conectividadTipo=DatabaseHelper.CONECTIVITY_TYPE_DISCONNECT;

		if(networkInfo == ConnectivityManager.TYPE_WIFI)
		{
			conectividadTipo=DatabaseHelper.CONECTIVITY_TYPE_WIFI;
		}
		else if(networkInfo == ConnectivityManager.TYPE_MOBILE)
		{
			conectividadTipo=DatabaseHelper.CONECTIVITY_TYPE_NETWORK;
		}

		dbHelper.insertarRegistroConectividad(Calendar.getInstance().getTime(), conectividadTipo, conectividadEstado);
	}

}
